package com.woniuxy.core.util;


import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 读取数据
     */
    public String get(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 写入数据
     */
    public void set(final String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    /**
     * 写入数据并设置有效时间
     */
    public void set(final String key, String value,long seconds) {
        stringRedisTemplate.opsForValue().set(key, value,seconds, TimeUnit.SECONDS);
    }
    /**
     * 删除数据
     */
    public void delete(final String key) {
        stringRedisTemplate.delete(key);
    }

    public void incre(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }

    public Boolean isCantReg(String key){
        return stringRedisTemplate.hasKey(key);

    }
    public Double zscroe(String key,String value){
        return stringRedisTemplate.opsForZSet().score(key,value);
    }

    public void  hset(String key, Map<Object,Object> map){
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    public long zcard(String key){
        return stringRedisTemplate.opsForZSet().size(key);
    }

    public Set<String> zrerange(String key, Integer start , Integer end){
        return   stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }

    public Set<String> zrerange1(String key, Integer start, Integer end) {
        Set<String> shoppingList = stringRedisTemplate.opsForZSet().reverseRange(key, start, end);

        // 创建一个新的集合来存储 foodId
        Set<String> foodIdSet = new HashSet<>();

        // 遍历购物车商品列表
        for (String shoppingCarKey : shoppingList) {
            // 提取 foodId
            String foodId = shoppingCarKey.substring(shoppingCarKey.lastIndexOf(":") + 1);
            // 将 foodId 添加到 foodIdSet 中
            foodIdSet.add(foodId);
        }

        // 返回 foodIdSet
        return foodIdSet;
    }


    public boolean exist(String key,String value) throws Exception{
        return stringRedisTemplate.opsForZSet().score(key,value)!=null;
    }
    public void srem(String key,String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key,value);
    }
    public void zadd(String key,String value,double score) throws Exception{
        stringRedisTemplate.opsForZSet().add(key,value,score);
    }

    public Double zscore(String key,String value) throws Exception{
       return stringRedisTemplate.opsForZSet().score(key,value);
    }
    public void zincrby(String key,String value,double score) throws Exception{
        stringRedisTemplate.opsForZSet().incrementScore(key,value,score);
    }

    public Long zTotal(String key) throws Exception{
        return stringRedisTemplate.opsForZSet().size(key);
    }
    //左闭右开
    public Set<String> zrevrange(String key, Integer start, Integer end) throws Exception{
        return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }
    public void zremove(String key, String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key,value);
    }
    public void del(String key,String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key,value);
    }


    public void del1(String key){
        stringRedisTemplate.delete(key);
    }
    //hset操作
    public void hincrby(String key,String property,Integer increment) throws Exception{
        stringRedisTemplate.opsForHash().increment(key,property,increment);
    }

    public void hmset(String key, HashMap<String, String> map) throws Exception{
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    public Map<Object,Object> hgetall(String key) throws Exception{
        return stringRedisTemplate.opsForHash().entries(key);
    }

    public void updateNumber(String key,String property,String value) throws Exception{
        stringRedisTemplate.opsForHash().put(key,property,value);
    }


    /**
     * 判断该键是否存在
     * */
    public boolean exists(final String key){
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 自增一
     */
    public void incr(final String key){
        stringRedisTemplate.opsForValue().increment(key);

    }

}

